package com.longge;

import java.util.HashSet;
import java.util.Set;

public class ToGoatLatin {

    public String toGoatLatin(String sentence) {

        String[] strs = sentence.split(" ");
        Set<Character> set = new HashSet<>();
        set.add('a');
        set.add('A');
        set.add('E');
        set.add('e');
        set.add('I');
        set.add('i');
        set.add('O');
        set.add('o');
        set.add('U');
        set.add('u');
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < strs.length; i++) {
            boolean flag = false;
            char oneChar = ' ';
            String str = strs[i];
            char[] chars = str.toCharArray();
            for (int j = 0; j < chars.length; j++) {
                if (j == 0 && set.contains(chars[j])) {
                    flag = true;
                    sb.append(chars[j]);
                }
                if (j == 0 && !set.contains(chars[j])) {
                    oneChar = chars[j];
                }
                if (j != 0&&j != chars.length - 1) {
                    sb.append(chars[j]);
                }
                if (j == chars.length - 1) {
                    if (j!=0){
                        sb.append(chars[j]);
                    }
                    if (!flag&&oneChar!=' ') {
                        sb.append(oneChar);
                    }
                    sb.append("ma");
                    int count = i + 1;
                    while (count != 0) {
                        count--;
                        sb.append("a");
                    }
                    if (i!=strs.length-1){

                        sb.append(' ');
                    }
                    flag = false;
                }
            }
        }
        return sb.toString();

    }

}
